package com.goods.business.service.imp;

import com.goods.business.converter.ProductConverter;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    //分页展示物资数据
    @Override
    public Map showProduct(Map pageMap) {
        List<Product> productList;
        String categorys = (String) pageMap.get("categorys");//categorys=33,34,37
        String name = (String) pageMap.get("name");
        String status = (String) pageMap.get("status");
        Long oneCategoryId = null;
        Long twoCategoryId = null;
        Long threeCategoryId = null;
        if (categorys==null && name==null && status==null) {
            productList = productMapper.selectAll();
        } else {
            if (!StringUtils.isEmpty(categorys)) {

                String[] split = StringUtils.split(categorys, ",");
                int length = split.length;
                switch (length) {
                    case 3 : threeCategoryId = Long.valueOf(split[2]);
                    case 2 : twoCategoryId = Long.valueOf(split[1]);
                    case 1 : oneCategoryId = Long.valueOf(split[0]);
                }
            }
            productList = productMapper.selectByNameCategoryIdStatus(name,status,oneCategoryId,twoCategoryId,threeCategoryId);
        }
        List<ProductVO> productVOList = ProductConverter.converterToProductVOList(productList);
        String pageSize = (String) pageMap.get("pageSize");
        String pageNum = (String) pageMap.get("pageNum");
        int pageSizeInt = productVOList.size();
        int pageNumInt = 1;
        if (pageSize!=null) {
            pageSizeInt = Integer.parseInt(pageSize);
        }
        if (pageNum!=null) {
            pageNumInt = Integer.parseInt(pageNum);
        }
        List<ProductVO> page = ListPageUtils.page(productVOList, pageSizeInt, pageNumInt);
        Map result = new HashMap();
        result.put("rows",page);
        result.put("total",productVOList.size());
        return result;
    }

    //添加
    @Override
    public void addProduct(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        product.setPNum(UUID.randomUUID().toString());
        product.setOneCategoryId(productVO.getCategoryKeys()[0]);
        product.setTwoCategoryId(productVO.getCategoryKeys()[1]);
        product.setThreeCategoryId(productVO.getCategoryKeys()[2]);
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());
        product.setStatus(0);
        productMapper.insert(product);
    }

    //回显编辑弹窗
    @Override
    public ProductVO showEdit(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        ProductVO productVO = ProductConverter.converterToProductVO(product);
        return productVO;
    }

    //修改
    @Override
    public void update(Long id, ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        product.setId(id);
        product.setOneCategoryId(productVO.getCategoryKeys()[0]);
        product.setTwoCategoryId(productVO.getCategoryKeys()[1]);
        product.setThreeCategoryId(productVO.getCategoryKeys()[2]);
        product.setModifiedTime(new Date());
        productMapper.updateByPrimaryKey(product);
    }

    //放入回收站
    @Override
    public void remove(Long id) {
        productMapper.updateStatus(id,Product.statusRemove);
    }

    //从回收站|审核恢复
    @Override
    public void backOrPublish(Long id) {
        productMapper.updateStatus(id,Product.statusNormal);
    }

    @Override
    public void delete(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }

}
